using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Event;
using NHibernate.Search;
using NHibernate.Search.Engine;
using NHibernate.Search.Impl;
using NHibernate.Tool.hbm2ddl;
using EasyCMMS.Services.Domain;
using EasyCMMS.Services.Repositories;
using NUnit.Framework;

namespace EasyCMMS.Tests.RepositoryTests
{
    public class NHibernateInMemoryTestFixtureBase
    {

        protected static ISessionFactory sessionFactory;

        protected static Configuration configuration;


        protected ISession session;



        [TestFixtureSetUp]
        public void OneTimeTestInitialize()
        {
            OneTimeInitalize(typeof(Customer).Assembly);
        }



        [SetUp]
        public void TestInitialize()
        {
            session = this.CreateSession();
        }



        [TearDown]
        public void TestCleanup()
        {
            session.Dispose();

        }



        /// <summary> 
        /// Initialize NHibernate and builds a session factory 
        /// Note, this is a costly call so it will be executed only once. 
        /// </summary> 
        public static void OneTimeInitalize(params Assembly[] assemblies)
        {

            if (sessionFactory != null)

                return;

            IDictionary<string, string> properties = new Dictionary<string, string>();

            properties.Add("connection.driver_class", "NHibernate.Driver.SQLite20Driver");

            properties.Add("dialect", "NHibernate.Dialect.SQLiteDialect");

            properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");

            properties.Add("connection.connection_string", "Data Source=:memory:;Version=3;New=True;"); 

            properties.Add("show_sql", "true");
            properties.Add("connection.release_mode", "on_close");
            properties.Add("search.default.directory_provider", "NHibernate.Search.Storage.FSDirectoryProvider, NHibernate.Search");
            properties.Add("search.default.indexBase", "index");

            configuration = new Configuration();
            configuration.EventListeners.SaveEventListeners = new ISaveOrUpdateEventListener[] { new CustomSaveEventListener() };
            configuration.EventListeners.UpdateEventListeners = new ISaveOrUpdateEventListener[] { new CustomUpdateEventListener() };



            configuration.Properties = properties;

            foreach (Assembly assembly in assemblies)
            {

                configuration = configuration.AddAssembly(assembly);

            }

            sessionFactory = configuration.BuildSessionFactory();

        }



        public ISession CreateSession()
        {

           ISession openSession = sessionFactory.OpenSession(); 

            IDbConnection connection = openSession.Connection; 

            new SchemaExport(configuration).Execute(false,true,false,true,connection,null);

            return openSession;



        }

 
        public void SetupDb()
        {
            //SqlCEDbHelper.CreateDatabaseFile("test.db");

            new SchemaExport(configuration).Execute(false, true, false, true);
        }


    }

}